Page({
  data: {
    username: '',
    password: '',
    email: ''
  },
//1234567
  inputUsername: function(e) {
    console.log('用户名:', e.detail.value);
    this.setData({username: e.detail.value});
  },
  inputPassword: function(e) {
    console.log('密码:', e.detail.value);
    this.setData({password: e.detail.value});
  },
  inputEmail: function(e) {
    console.log('邮箱:', e.detail.value);
    this.setData({email: e.detail.value});
  },

  register: function() {
    console.log('注册数据:', this.data);
    if(!this.data.username || !this.data.password || !this.data.email) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    wx.request({
      url: 'http://81.70.199.174:8081/user/register',
      method: 'POST',
      data: {
        username: this.data.username,
        password: this.data.password,
        email: this.data.email
      },
      success(res) {
        console.log('注册响应:', res);
        if(res.data.code === 200) {
          wx.showToast({
            title:res.data.message, // '注册成功',
            icon: 'success',
            duration: 2000
          });
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/login/login'
            });
          }, 2000);
        } else {
          wx.showToast({
            title: res.data.message || '注册失败',
            icon: 'none',
            duration: 2000
          });
        }
      },
      fail(err) {
        console.error('请求失败:', err);
        wx.showToast({
          title: '网络错误',
          icon: 'none',
          duration: 2000
        });
      }
    });
  },
  register1:function(){
    if(!this.data.username||!this.data.password||!this.data.email){
      wx.showToast({
        title: '请输入完整信息',
        icon:'error',
        duration:2000
      });
      return;
    }
    wx.request({
      url:'http://81.70.199.174:8081/user/register',
      method:'POST',
      data:{
        username:this.data.username,
        password:this.data.password,
        email:this.data.email
      },
      success(res){
        console.log(res);
        if(res.data.code===200){
          wx.showToast({
            title: '注册成功',
            icon:'success',
            duration:2000
          });
          setTimeout(()=>{
            wx.navigateTo({
              url: '/pages/login/login'
            });
          },2000);
          const app=getApp();
          app.globalData.userInfo={
            username:this.data.username,
            password:this.data.password,
            email:this.data.email,
            userId:1,
          }
        }else{
          wx.showToast({
            title: res.data.message||'注册失败',
            icon:'error',
            duration:2000
          });
        }
      },
      fail(err){
        console.error('请求失败:', err);
        wx.showToast({
          title:'注册失败',
          icon:'error',
          duration:2000
        });
      }
    });
  }
});